//
//  GGRouteHandler.m
//  qqqqqq
//
//  Created by iOSer on 2017/3/2.
//  Copyright © 2017年 iOSer. All rights reserved.
//



#import "GGRouteHandler.h"
#import "UIViewController+GGRoute.h"
#import "GGRouteRequest.h"
#import "NSError+GGRouteError.h"
#import "GGNavViewcontroller.h"
static NSString *routeConfigName = @"GGRouteConfig";

@implementation GGRouteHandler

-(BOOL)shouldHandleWithRequest:(GGRouteRequest *)request{
    return YES;
}
-(UIViewController *)targetViewControllerWithRequest:(GGRouteRequest *)request{
    return [[NSClassFromString(@"HBTestViewController") alloc] init];
}
////获得当前准确控制器
-(UIViewController *)nowSourceViewController{
    UIWindow *window = [[UIApplication sharedApplication].delegate window];
    if (!window) {
        return nil;
    }
    UIView *tempView;
    for (UIView *subview in window.subviews) {
        if ([[subview.classForCoder description] isEqualToString:@"UILayoutContainerView"]) {
            tempView = subview;
            break;
        }
    }
    if (!tempView) {
        tempView = [window.subviews lastObject];
    }
    
    id nextResponder = [tempView nextResponder];
    while (![nextResponder isKindOfClass:[UIViewController class]] || [nextResponder isKindOfClass:[UINavigationController class]] || [nextResponder isKindOfClass:[UITabBarController class]]) {
        tempView =  [tempView.subviews firstObject];
        
        if (!tempView) {
            return nil;
        }
        nextResponder = [tempView nextResponder];
    }
    return  (UIViewController *)nextResponder;
}
//-(id)getCurrentVC
//{
//    UIViewController *result = nil;
//    UIWindow * window = [[UIApplication sharedApplication] keyWindow];
//    //app默认windowLevel是UIWindowLevelNormal，如果不是，找到UIWindowLevelNormal的
//    if (window.windowLevel != UIWindowLevelNormal)
//    {
//        NSArray *windows = [[UIApplication sharedApplication] windows];
//        for(UIWindow * tmpWin in windows)
//        {
//            if (tmpWin.windowLevel == UIWindowLevelNormal)
//            {
//                window = tmpWin;
//                break;
//            }
//        }
//    }
//    id  nextResponder = nil;
//    UIViewController *appRootVC=window.rootViewController;
//    //    如果是present上来的appRootVC.presentedViewController 不为nil
//    if (appRootVC.presentedViewController) {
//        nextResponder = appRootVC.presentedViewController;
//    }else{
//        
//        NSLog(@"===%@",[window subviews]);
//        UIView *frontView = [[window subviews] objectAtIndex:0];
//        nextResponder = [frontView nextResponder];
//    }
//    
////    while (![nextResponder isKindOfClass:[UIViewController class]] || [nextResponder isKindOfClass:[UINavigationController class]] || [nextResponder isKindOfClass:[UITabBarController class]]) {
////        tempView =  [tempView.subviews firstObject];
////        
////        if (!tempView) {
////            return nil;
////        }
////        nextResponder = [tempView nextResponder];
////    }
//    if ([nextResponder isKindOfClass:[UITabBarController class]]){
//        UITabBarController * tabbar = (UITabBarController *)nextResponder;
//        UINavigationController * nav = (UINavigationController *)tabbar.viewControllers[tabbar.selectedIndex];
//        //        UINavigationController * nav = tabbar.selectedViewController ; 上下两种写法都行
//        result=nav;
//        
//    }else if ([nextResponder isKindOfClass:[UINavigationController class]]){
//        UINavigationController * nav = nextResponder;
//        result = nav;
//    }else{
//        result = nextResponder;
//    }
//    return result;   
//}
////获得Main控制器
//-(UIViewController *)sourceViewControllerForTransitionWithRequest:(GGRouteRequest *)request{
//   
//    return [UIApplication sharedApplication].windows[0].rootViewController;
//}

-(BOOL)handleRequest:(GGRouteRequest *)request error:(NSError *__autoreleasing *)error{
    
//    NSString *rootVCName = data[@"RootViewController"];
//    UIViewController *mainViewController = [self sourceViewControllerForTransitionWithRequest:request];
//    
//    if ([mainViewController isKindOfClass:[UITabBarController class]]) {
//        UITabBarController *tabbarControl = (UITabBarController *)mainViewController;
//       id viewcontroller = tabbarControl.selectedViewController;
//        
//    }
    UIViewController *sourceViewController = [self nowSourceViewController];
    
//    NSArray *viewcontrollers = sourceViewController.navigationController.viewControllers;
//    if (viewcontrollers.count>1) {
//        if ([viewcontrollers objectAtIndex:viewcontrollers.count-1]==sourceViewController) {
//            self.sourceWithType = sourceWithPush;
//            //push方式
//        }
//    }
//    else{
//        
//        self.sourceWithType = sourceWithPresent;
//        //present方式
//    }
    
//    if (![[[DefaultTools sharedInstance] getCurrentVC] isKindOfClass:[NSClassFromString(rootVCName) class]]) {
//        sourceViewController = [[DefaultTools sharedInstance] getCurrentVC];
//    }
//    id sourceViewController = [self getCurrentVC];
    
    UIViewController *targetViewController = [self targetViewControllerWithRequest:request];
    if ((![sourceViewController isKindOfClass:[UIViewController class]]) || (![targetViewController isKindOfClass:[UIViewController class]])) {
        *error = [NSError GGTransitionError];
        return NO;
    }
    if (targetViewController != nil) {
        targetViewController.gg_request = request;
    }
    BOOL isPreferModal = [self preferModalPresentationWithRequest:request];
    return [self transitionWithRequest:request sourceViewController:sourceViewController targetViewController:targetViewController isPreferModal:isPreferModal error:error];
}

-(BOOL)transitionWithRequest:(GGRouteRequest *)request sourceViewController:(UIViewController *)sourceViewController targetViewController:(UIViewController *)targetViewController isPreferModal:(BOOL)isPreferModal error:(NSError *__autoreleasing *)error{
    //设置导航条标题
    if (request.navTitle) {
        targetViewController.navigationItem.title = request.navTitle;
    }
    if (request.jumpMethod == PUSH){
        
        [sourceViewController.navigationController pushViewController:targetViewController animated:request.isNavAnimation];

    }else{
        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:targetViewController];
        [sourceViewController presentViewController:nav animated:request.isNavAnimation completion:nil];
    }

    return YES;
}

-(BOOL)preferModalPresentationWithRequest:(GGRouteRequest *)request{
    return NO;
}
@end
